<?php
//phpinfo();
//exit;
/* 배열정보를 토대로 클래스 생성 및 메소드 호출
 * */
include_once $_SERVER['DOCUMENT_ROOT'].'/common/util/common.php';

class classLoader{
	private $serviceId;
	private $result;
	private $classInfo;
	private $func = array();
	private $params;
	
	function __construct(){
		//$this->classInfo = json_decode($_POST['classInfo'], true);
		//jsondecode를 array로 지정해도 타입에러가 나서 배열에 다시 담음
		//$this->func = array($this->classInfo['class'], $this->classInfo['method']);
		//$this->paramsInfo = json_decode($_POST['params'], true);
		//var_dump($this->paramsInfo);
		//unset($_POST);
		
		$this->getService();
	}
	
	public function getService(){
		$this->serviceId = $_POST["serviceId"];
		
		$param = array();
		$param['serviceId'] = $this->serviceId;
	
		/*서비스ID 조회*/
		$fpBatis = new FPBatis(SQL_MAP_URL);
		$fpBatis->setDebug(false);
		$result = $fpBatis->doSelect("Common.selectServiceId", $param);
	
		$this->classInfo =  array(
				"class"		=> $result[0]['classNm'],
				"method"	=> $result[0]['methodNm']
		);
		if(is_array($_POST[params])){
			$this->params = $_POST[params];
		}else{
			$this->params = decode_items(getParamToArray($_POST['params']));
		}
	}
	
	/*클래스 생성 및 메소드 호출 후 결과 리턴
	 * param : $classInfo = array('class'=>'class', 'method'=>'method')
	 * */
	public function Loader(){
		
		try{
		/* 	if(!trim($this->classInfo)){
			throw new Exception();
		} */
			
		if(!is_null($this->classInfo['class']) && !is_null($this->classInfo['method'])){
			$this->result = call_user_func(array($this->classInfo['class'], $this->classInfo['method']), $this->params);
			logging($this->params);
		}else{
			$this->result = "서비스 없음";
		}
			
		}catch(Exception $e){
			var_dump($e->getTraceAsString());
		}
	}
	
	//private function paramToArray
	
	/* 동적 클래스 로딩후 값을 반환해준다  
	 * 
	 * */
	function __destruct(){
		$typeDef = array("object","array");
		
		if(in_array(gettype($this->result), $typeDef)){
			$result = $this->result;
			$result = encode_items($result);
			//5.2버전 한글 버그 때문에 urlencode 처리뒤 json_encode
			echo urldecode(json_encode($result));
			
		}else{
			echo $this->result;
		}
	}
}

//$classInfo = json_decode($_POST);
$classLoader = new classLoader();
$classLoader->Loader();

?>
